/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.suren.autotest.interfaces.framework.data;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 * 根据swagger中的json schema生成数据
 * @author suren
 * @date 2017年7月3日 下午4:56:13
 */
public class SwaggerJsonValue implements SwaggerJsonDynamicValue, SwaggerJsonDefinition
{
	private String def;
	private DynamicValue dynamicValue = new SimpleDynamicValue();

	@Override
	public String getDefinition()
	{
		return this.def;
	}

	@Override
	public String getJsonValue(String ref)
	{
		String key = ref.replace("#/definitions/", "");
		
        JsonParser jsonParser = new JsonParser();
        JsonObject jsonObject = (JsonObject) jsonParser.parse(def);
        
        JsonObject resultObj = new JsonObject();
        
        JsonObject defObj = (JsonObject) jsonObject.get("definitions");
        if(defObj.has(key))
        {
        	defObj = (JsonObject) defObj.get(key);
        	
        	if(defObj.has("properties"))
        	{
        		JsonObject pros =  (JsonObject) defObj.get("properties");
        		
        		pros.entrySet().forEach((entry) -> {
        			String name = entry.getKey();
        			String value = null;
        			
        			JsonElement typeObj = ((JsonObject) entry.getValue()).get("type");
        			if(typeObj != null)
        			{
        				String type = typeObj.getAsString();
        				if("array".equals(type))
        				{
        					JsonObject itemsObj = (JsonObject) ((JsonObject) entry.getValue()).get("items");
        					
        					value = getJsonValue(itemsObj.get("$ref").getAsString());
        				}
        				else
        				{
                			value = dynamicValue.getValue(type);
        				}
        			}
        			else
        			{
        				value = getJsonValue(((JsonObject) entry.getValue()).get("$ref").getAsString());
        			}
        			
        			resultObj.addProperty(name, value);
        		});
        	}
        }
        
		return resultObj.toString();
	}

	@Override
	public void setDefinition(String def)
	{
		this.def = def;
	}

	public static void main(String[] args)
	{
		SwaggerJsonValue swaggerJsonValue = new SwaggerJsonValue();
		swaggerJsonValue.setDefinition("{'swagger':'2.0','info':{},'host':'192.168.94.61:8080','basePath':'/cost','tags':[{'name':'cost-quantity-price-bill-controller','description':'量价文件单据'},{'name':'cost-association-controller','description':'Cost Association Controller'},{'name':'cost-model-controller','description':'集成模型成本'}],'paths':{'/D/{deptId}/cost/models/{projectVersionId}/bqitems':{'get':{'tags':['cost-model-controller'],'summary':'查询清单','operationId':'searchBQItemByTradeNameUsingGET','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'deptId','in':'path','description':'deptId','required':true,'type':'integer','format':'int64'},{'name':'keywords','in':'query','description':'keywords','required':false,'type':'string'},{'name':'buildingId','in':'query','description':'buildingId','required':true,'type':'string'},{'name':'projectVersionId','in':'path','description':'projectVersionId','required':true,'type':'string'},{'name':'tradeName','in':'query','description':'tradeName','required':false,'type':'string'},{'name':'page','in':'query','description':'页索引','required':true,'type':'integer','format':'int32'},{'name':'size','in':'query','description':'页数量','required':true,'type':'integer','format':'int32'},{'name':'withPageCount','in':'query','description':'是否查询总数','required':true,'type':'boolean'}],'responses':{'200':{'description':'OK','schema':{'$ref':'#/definitions/Page«CostBQItemProps»'}},'401':{'description':'Unauthorized'},'403':{'description':'Forbidden'},'404':{'description':'Not Found'}}}},'/D/{deptId}/cost/models/{projectVersionId}/bqitems/changes':{'get':{'tags':['cost-model-controller'],'summary':'查询变更清单','operationId':'bqItemChangesUsingGET','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'deptId','in':'path','description':'deptId','required':true,'type':'integer','format':'int64'},{'name':'keywords','in':'query','description':'检索条件','required':false,'type':'string'},{'name':'buildingId','in':'query','description':'buildingId','required':false,'type':'integer','format':'int64'},{'name':'projectVersionId','in':'path','description':'projectVersionId','required':true,'type':'string'},{'name':'page','in':'query','description':'页索引','required':true,'type':'integer','format':'int32'},{'name':'size','in':'query','description':'页数量','required':true,'type':'integer','format':'int32'},{'name':'withPageCount','in':'query','description':'是否查询总数','required':true,'type':'boolean'}],'responses':{'200':{'description':'OK','schema':{'$ref':'#/definitions/Page«CostBQItemChangeInfo»'}},'401':{'description':'Unauthorized'},'403':{'description':'Forbidden'},'404':{'description':'Not Found'}}}},'/D/{deptId}/cost/models/{projectVersionId}/bqitems/unrelated':{'get':{'tags':['cost-model-controller'],'summary':'未关联清单','operationId':'searchUnrelatedBQItemUsingGET','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'deptId','in':'path','description':'deptId','required':true,'type':'integer','format':'int64'},{'name':'projectVersionId','in':'path','description':'projectVersionId','required':true,'type':'string'}],'responses':{'200':{'description':'OK','schema':{'type':'array','items':{'$ref':'#/definitions/CostBQItemUnrelatedBQItemInfo'}}},'401':{'description':'Unauthorized'},'403':{'description':'Forbidden'},'404':{'description':'Not Found'}}}},'/D/{deptId}/cost/models/{projectVersionId}/bqitems/{bqitemId}/elements':{'get':{'tags':['cost-model-controller'],'summary':'根据清单取相关构件','operationId':'findElementByBQItemIdUsingGET','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'deptId','in':'path','description':'deptId','required':true,'type':'integer','format':'int64'},{'name':'projectVersionId','in':'path','description':'projectVersionId','required':true,'type':'string'},{'name':'bqitemId','in':'path','description':'bqitemId','required':true,'type':'string'},{'name':'buildingId','in':'query','description':'buildingId','required':true,'type':'integer','format':'int64'},{'name':'page','in':'query','description':'页索引','required':true,'type':'integer','format':'int32'},{'name':'size','in':'query','description':'页数量','required':true,'type':'integer','format':'int32'},{'name':'withPageCount','in':'query','description':'是否查询总数','required':true,'type':'boolean'}],'responses':{'200':{'description':'OK','schema':{'$ref':'#/definitions/Page«CostBQItemElementInfo»'}},'401':{'description':'Unauthorized'},'403':{'description':'Forbidden'},'404':{'description':'Not Found'}}}},'/D/{deptId}/cost/models/{projectVersionId}/elements/unrelated':{'get':{'tags':['cost-model-controller'],'summary':'根据分类查询未关联构件','operationId':'searchUnrelatedElementsByTypeUsingGET','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'deptId','in':'path','description':'deptId','required':true,'type':'integer','format':'int64'},{'name':'buildingId','in':'query','description':'buildingId','required':true,'type':'integer','format':'int64'},{'name':'elementTypeId','in':'query','description':'elementTypeId','required':true,'type':'string'},{'name':'projectVersionId','in':'path','description':'projectVersionId','required':true,'type':'string'},{'name':'page','in':'query','description':'页索引','required':true,'type':'integer','format':'int32'},{'name':'size','in':'query','description':'页数量','required':true,'type':'integer','format':'int32'},{'name':'withPageCount','in':'query','description':'是否查询总数','required':true,'type':'boolean'}],'responses':{'200':{'description':'OK','schema':{'$ref':'#/definitions/Page«CostBQItemUnrelatedElementInfo»'}},'401':{'description':'Unauthorized'},'403':{'description':'Forbidden'},'404':{'description':'Not Found'}}}},'/D/{deptId}/cost/models/{projectVersionId}/elements/unrelated/elementtypesummary':{'get':{'tags':['cost-model-controller'],'summary':'未关联构件类型汇总','operationId':'searchUnrelatedElementsUsingGET','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'deptId','in':'path','description':'deptId','required':true,'type':'integer','format':'int64'},{'name':'buildingId','in':'query','description':'buildingId','required':true,'type':'integer','format':'int64'},{'name':'projectVersionId','in':'path','description':'projectVersionId','required':true,'type':'string'}],'responses':{'200':{'description':'OK','schema':{'type':'array','items':{'$ref':'#/definitions/CostBQItemUnrelatedElementTypeCountInfo'}}},'401':{'description':'Unauthorized'},'403':{'description':'Forbidden'},'404':{'description':'Not Found'}}}},'/D/{deptId}/cost/models/{projectVersionId}/elements/{elementId}/bqitems':{'get':{'tags':['cost-model-controller'],'summary':'根据构件取相关清单','operationId':'findBQItemByElementIdUsingGET','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'deptId','in':'path','description':'deptId','required':true,'type':'integer','format':'int64'},{'name':'projectVersionId','in':'path','description':'projectVersionId','required':true,'type':'string'},{'name':'elementId','in':'path','description':'elementId','required':true,'type':'string'},{'name':'buildingId','in':'query','description':'buildingId','required':true,'type':'integer','format':'int64'},{'name':'page','in':'query','description':'页索引','required':true,'type':'integer','format':'int32'},{'name':'size','in':'query','description':'页数量','required':true,'type':'integer','format':'int32'},{'name':'withPageCount','in':'query','description':'是否查询总数','required':true,'type':'boolean'}],'responses':{'200':{'description':'OK','schema':{'$ref':'#/definitions/Page«CostBQItemInfo»'}},'401':{'description':'Unauthorized'},'403':{'description':'Forbidden'},'404':{'description':'Not Found'}}}},'/D/{deptId}/cost/models/{projectVersionId}/trades':{'get':{'tags':['cost-model-controller'],'summary':'清单专业','operationId':'searchBQItemTradeNameUsingGET','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'deptId','in':'path','description':'deptId','required':true,'type':'integer','format':'int64'},{'name':'keywords','in':'query','description':'keywords','required':false,'type':'string'},{'name':'buildingId','in':'query','description':'buildingId','required':true,'type':'string'},{'name':'projectVersionId','in':'path','description':'projectVersionId','required':true,'type':'string'}],'responses':{'200':{'description':'OK','schema':{'type':'array','items':{'$ref':'#/definitions/CostBQItemTradeProps'}}},'401':{'description':'Unauthorized'},'403':{'description':'Forbidden'},'404':{'description':'Not Found'}}}},'/D/{deptId}/quantityprices':{'get':{'tags':['cost-quantity-price-bill-controller'],'summary':'获取某个项目下所有的量价文件','operationId':'getByProjectIdUsingGET','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'deptId','in':'path','description':'deptId','required':true,'type':'integer','format':'int64'},{'name':'page','in':'query','description':'页索引','required':true,'type':'integer','format':'int32'},{'name':'size','in':'query','description':'页数量','required':true,'type':'integer','format':'int32'}],'responses':{'200':{'description':'OK','schema':{'$ref':'#/definitions/Page«QuantityPriceBillInfo»'}},'401':{'description':'Unauthorized'},'403':{'description':'Forbidden'},'404':{'description':'Not Found'}}},'post':{'tags':['cost-quantity-price-bill-controller'],'summary':'新增量价文件单据','operationId':'addQuantityPriceBillUsingPOST','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'deptId','in':'path','description':'deptId','required':true,'type':'integer','format':'int64'},{'in':'body','name':'quantityPriceBillProps','description':'量价文件单据','required':false,'schema':{'$ref':'#/definitions/QuantityPriceBillProps'}}],'responses':{'200':{'description':'OK','schema':{'$ref':'#/definitions/IdResult'}},'201':{'description':'Created'},'401':{'description':'Unauthorized'},'403':{'description':'Forbidden'},'404':{'description':'Not Found'}}}},'/D/{deptId}/quantityprices/{id}':{'get':{'tags':['cost-quantity-price-bill-controller'],'summary':'获取单个量价文件详细信息','operationId':'getDetailByIdUsingGET','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'deptId','in':'path','description':'deptId','required':true,'type':'integer','format':'int64'},{'name':'id','in':'path','description':'量价文件ID','required':true,'type':'integer','format':'int64'}],'responses':{'200':{'description':'OK','schema':{'$ref':'#/definitions/QuantityPriceBillInfoWithDetail'}},'401':{'description':'Unauthorized'},'403':{'description':'Forbidden'},'404':{'description':'Not Found'}}},'put':{'tags':['cost-quantity-price-bill-controller'],'summary':'更新量价文件单据','operationId':'updateQuantityPriceUsingPUT','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'deptId','in':'path','description':'deptId','required':true,'type':'integer','format':'int64'},{'name':'id','in':'path','description':'量价单据ID','required':true,'type':'integer','format':'int64'},{'in':'body','name':'quantityPriceBillProps','description':'量价文件单据','required':false,'schema':{'$ref':'#/definitions/QuantityPriceBillProps'}}],'responses':{'200':{'description':'OK'},'201':{'description':'Created'},'401':{'description':'Unauthorized'},'403':{'description':'Forbidden'},'404':{'description':'Not Found'}}},'delete':{'tags':['cost-quantity-price-bill-controller'],'summary':'根据量价单据ID删除','operationId':'delByIdUsingDELETE','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'id','in':'path','description':'量价文件ID','required':true,'type':'integer','format':'int64'}],'responses':{'200':{'description':'OK'},'401':{'description':'Unauthorized'},'204':{'description':'No Content'},'403':{'description':'Forbidden'}}}},'/cost/{projectId}/{projectVersion}/associations':{'post':{'tags':['cost-association-controller'],'summary':'发起关联','operationId':'startAssociationUsingPOST','consumes':['application/json'],'produces':['*/*'],'parameters':[{'name':'projectId','in':'path','description':'projectId','required':true,'type':'integer','format':'int64'},{'name':'projectVersion','in':'path','description':'projectVersion','required':true,'type':'string'}],'responses':{'200':{'description':'OK','schema':{'$ref':'#/definitions/IdResult'}},'201':{'description':'Created'},'401':{'description':'Unauthorized'},'403':{'description':'Forbidden'},'404':{'description':'Not Found'}}}}},'definitions':{'CostBQItemUnrelatedBQItemInfo':{'type':'object','properties':{'bqcode':{'type':'string'},'bqname':{'type':'string'},'buildingId':{'type':'integer','format':'int64'},'quantity':{'type':'number'},'rate':{'type':'number'},'spec':{'type':'string'},'tradeName':{'type':'string'},'unit':{'type':'string'}}},'Page«QuantityPriceBillInfo»':{'type':'object','properties':{'content':{'type':'array','items':{'$ref':'#/definitions/QuantityPriceBillInfo'}},'first':{'type':'boolean'},'last':{'type':'boolean'},'number':{'type':'integer','format':'int32'},'numberOfElements':{'type':'integer','format':'int32'},'size':{'type':'integer','format':'int32'},'sort':{'$ref':'#/definitions/Sort'},'totalElements':{'type':'integer','format':'int64'},'totalPages':{'type':'integer','format':'int32'}}},'CostBQItemProps':{'type':'object','properties':{'bqcode':{'type':'string'},'buildingId':{'type':'integer','format':'int64'},'id':{'type':'integer','format':'int64'},'itemType':{'type':'string'},'name':{'type':'string'},'pid':{'type':'integer','format':'int64'},'quantity':{'type':'number'},'rate':{'type':'number'},'spec':{'type':'string'},'tradeName':{'type':'string'},'unit':{'type':'string'}}},'Page«CostBQItemInfo»':{'type':'object','properties':{'content':{'type':'array','items':{'$ref':'#/definitions/CostBQItemInfo'}},'first':{'type':'boolean'},'last':{'type':'boolean'},'number':{'type':'integer','format':'int32'},'numberOfElements':{'type':'integer','format':'int32'},'size':{'type':'integer','format':'int32'},'sort':{'$ref':'#/definitions/Sort'},'totalElements':{'type':'integer','format':'int64'},'totalPages':{'type':'integer','format':'int32'}}},'QuantityPriceBillProps':{'type':'object','properties':{'code':{'type':'string'},'contractChange':{'type':'string'},'createDate':{'type':'string','format':'date-time'},'creatorId':{'type':'integer','format':'int64'},'creatorName':{'type':'string'},'modelVersion':{'type':'string'},'modelVersionId':{'type':'string'},'projectId':{'type':'integer','format':'int64'},'projectName':{'type':'string'},'quantityPriceFileInfoList':{'type':'array','items':{'$ref':'#/definitions/QuantityPriceFileProps'}},'remark':{'type':'string'}}},'Page«CostBQItemProps»':{'type':'object','properties':{'content':{'type':'array','items':{'$ref':'#/definitions/CostBQItemProps'}},'first':{'type':'boolean'},'last':{'type':'boolean'},'number':{'type':'integer','format':'int32'},'numberOfElements':{'type':'integer','format':'int32'},'size':{'type':'integer','format':'int32'},'sort':{'$ref':'#/definitions/Sort'},'totalElements':{'type':'integer','format':'int64'},'totalPages':{'type':'integer','format':'int32'}}},'QuantityPriceBillInfoWithDetail':{'type':'object','properties':{'code':{'type':'string'},'contractChange':{'type':'string'},'createDate':{'type':'string','format':'date-time'},'creatorId':{'type':'integer','format':'int64'},'creatorName':{'type':'string'},'id':{'type':'integer','format':'int64'},'modelVersion':{'type':'string'},'modelVersionId':{'type':'string'},'projectId':{'type':'integer','format':'int64'},'projectName':{'type':'string'},'quantityPriceFileInfoList':{'type':'array','items':{'$ref':'#/definitions/QuantityPriceFileInfo'}},'remark':{'type':'string'}}},'IdResult':{'type':'object','properties':{'id':{'type':'integer','format':'int64'}}},'QuantityPriceFileInfo':{'type':'object','properties':{'createTime':{'type':'string','format':'date-time'},'fileId':{'type':'string'},'id':{'type':'integer','format':'int64'},'length':{'type':'integer','format':'int64'},'name':{'type':'string'},'priceBillId':{'type':'integer','format':'int64'},'quantityPriceFileTypeInfos':{'type':'array','items':{'$ref':'#/definitions/QuantityPriceFileTypeInfo'}},'remark':{'type':'string'},'updateTime':{'type':'string','format':'date-time'},'version':{'type':'integer','format':'int32'}}},'Page«CostBQItemChangeInfo»':{'type':'object','properties':{'content':{'type':'array','items':{'$ref':'#/definitions/CostBQItemChangeInfo'}},'first':{'type':'boolean'},'last':{'type':'boolean'},'number':{'type':'integer','format':'int32'},'numberOfElements':{'type':'integer','format':'int32'},'size':{'type':'integer','format':'int32'},'sort':{'$ref':'#/definitions/Sort'},'totalElements':{'type':'integer','format':'int64'},'totalPages':{'type':'integer','format':'int32'}}},'CostBQItemUnrelatedElementInfo':{'type':'object','properties':{'elementId':{'type':'string'},'elementName':{'type':'string'}}},'Page«CostBQItemElementInfo»':{'type':'object','properties':{'content':{'type':'array','items':{'$ref':'#/definitions/CostBQItemElementInfo'}},'first':{'type':'boolean'},'last':{'type':'boolean'},'number':{'type':'integer','format':'int32'},'numberOfElements':{'type':'integer','format':'int32'},'size':{'type':'integer','format':'int32'},'sort':{'$ref':'#/definitions/Sort'},'totalElements':{'type':'integer','format':'int64'},'totalPages':{'type':'integer','format':'int32'}}},'CostBQItemUnrelatedElementTypeCountInfo':{'type':'object','properties':{'count':{'type':'integer','format':'int32'},'elementTypeId':{'type':'integer','format':'int32'},'elementTypeName':{'type':'string'}}},'QuantityPriceBillInfo':{'type':'object','properties':{'code':{'type':'string'},'contractChange':{'type':'string'},'createDate':{'type':'string','format':'date-time'},'creatorId':{'type':'integer','format':'int64'},'creatorName':{'type':'string'},'id':{'type':'integer','format':'int64'},'modelVersion':{'type':'string'},'modelVersionId':{'type':'string'},'projectId':{'type':'integer','format':'int64'},'projectName':{'type':'string'},'remark':{'type':'string'}}},'CostBQItemInfo':{'type':'object','properties':{'bqcode':{'type':'string'},'buildingId':{'type':'integer','format':'int64'},'id':{'type':'integer','format':'int64'},'itemType':{'type':'string'},'name':{'type':'string'},'pid':{'type':'integer','format':'int64'},'quantity':{'type':'number'},'rate':{'type':'number'},'spec':{'type':'string'},'tradeName':{'type':'string'},'unit':{'type':'string'}}},'Sort':{'type':'object'},'CostBQItemTradeProps':{'type':'object','properties':{'tradeName':{'type':'string'}}},'Page«CostBQItemUnrelatedElementInfo»':{'type':'object','properties':{'content':{'type':'array','items':{'$ref':'#/definitions/CostBQItemUnrelatedElementInfo'}},'first':{'type':'boolean'},'last':{'type':'boolean'},'number':{'type':'integer','format':'int32'},'numberOfElements':{'type':'integer','format':'int32'},'size':{'type':'integer','format':'int32'},'sort':{'$ref':'#/definitions/Sort'},'totalElements':{'type':'integer','format':'int64'},'totalPages':{'type':'integer','format':'int32'}}},'CostBQItemElementInfo':{'type':'object','properties':{'bqitemId':{'type':'integer','format':'int64'},'elementId':{'type':'string'},'elementName':{'type':'string'},'elementTypeId':{'type':'integer','format':'int32'},'elementTypeName':{'type':'string'},'levelName':{'type':'string'},'quantity':{'type':'number'},'specialtyId':{'type':'integer','format':'int64'}}},'QuantityPriceFileProps':{'type':'object','properties':{'createTime':{'type':'string','format':'date-time'},'fileId':{'type':'string'},'length':{'type':'integer','format':'int64'},'name':{'type':'string'},'priceBillId':{'type':'integer','format':'int64'},'quantityPriceFileTypeInfos':{'type':'array','items':{'$ref':'#/definitions/QuantityPriceFileTypeInfo'}},'remark':{'type':'string'},'updateTime':{'type':'string','format':'date-time'},'version':{'type':'integer','format':'int32'}}},'QuantityPriceFileTypeInfo':{'type':'object','properties':{'buildingId':{'type':'integer','format':'int64'},'buildingName':{'type':'string'},'specialtyId':{'type':'integer','format':'int64'},'specialtyName':{'type':'string'}}},'CostBQItemChangeInfo':{'type':'object','properties':{'afterQuantity':{'type':'number'},'beforeQuantity':{'type':'number'},'bqcode':{'type':'string'},'deltaQuantity':{'type':'number'},'id':{'type':'integer','format':'int64'},'name':{'type':'string'},'rate':{'type':'number'},'spec':{'type':'string'},'unit':{'type':'string'}}}}}");
		String result = swaggerJsonValue.getJsonValue("#/definitions/Page«QuantityPriceBillInfo»");
		System.out.println(result);
	}
}
